package com.hucheng.user.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.hucheng.common.constant.RedisConstant;
import com.hucheng.common.constant.UserConstant;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.utils.DesUtils;
import com.hucheng.common.utils.RedisUtils;
import com.hucheng.user.entity.UserDO;
import com.hucheng.user.mapper.UserMapper;
import com.hucheng.user.query.UserQuery;
import com.hucheng.user.service.UserService;
import com.hucheng.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-03
 * @Description: 暴露服务 关闭重试 超时时间5ms
 **/
@Component
@Service(interfaceClass = UserService.class, retries = -1, timeout = 5000)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public UserVO findUserByName(UserQuery userQuery) throws EduException {
        UserDO userDO = userMapper.getUserByName(userQuery);
        if (userDO == null) {
            throw new EduException(110001);
        }
        String userKey = DesUtils.encrypt(userDO.getUserId()+"");
        UserVO userVO = RedisUtils.get(stringRedisTemplate, RedisConstant.HSET_USER_SESSION_KEY + userKey, UserVO.class);
        if (userVO != null) {
            //更新会话时间
            RedisUtils.set(stringRedisTemplate, RedisConstant.HSET_USER_SESSION_KEY + userKey, JSONObject.toJSONString(userVO), UserConstant.TOKEN_EXPIRE);
            return userVO;
        }

        if (!userDO.getPassword().equals(DesUtils.encrypt(userQuery.getPassword()))) {
            throw new EduException(110002);
        }
        String accessId = UUID.randomUUID().toString().replaceAll("-", "");
        userVO = new UserVO();
        userVO.setUserId(userDO.getUserId());
        userVO.setUsername(userDO.getUsername());
        userVO.setAccessId(accessId);
        RedisUtils.set(stringRedisTemplate, RedisConstant.HSET_USER_SESSION_KEY + userKey, JSONObject.toJSONString(userVO), UserConstant.TOKEN_EXPIRE);
        return userVO;
    }

    @Override
    public void logout(Integer userId) {
        String userKey = DesUtils.encrypt(userId+"");
        RedisUtils.del(stringRedisTemplate, RedisConstant.HSET_USER_SESSION_KEY + userKey);
    }

//
//    @Override
//    public CommonPager<UserVO> listByPage(UserQuery userQuery) {
//        PageParameter pageParameter = userQuery.getPageParameter();
//        return new CommonPager<>(
//                new PageParameter(pageParameter.getCurrentPage(),
//                        pageParameter.getPageSize(), userMapper.countByQuery(userQuery)),
//                userMapper.selectByQuery(userQuery).stream()
//                        .map(UserVO::buildUserVO)
//                        .collect(Collectors.toList()));
//    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Integer createOrUpdate(final TitlesDTO titlesDTO) {
//        int titleCount;
//        TitlesDO titlesDO = TitlesDO.buildTitlesDO(titlesDTO);
//        if (StringUtils.isEmpty(titlesDTO.getEmpNo())) {
//            titleCount = titlesMapper.insertSelective(titlesDO);
//        } else {
//            titleCount = titlesMapper.updateSelective(titlesDO);
//        }
//        return titleCount;
//    }


}
